<?php
class FunctionGroup{
	static function addSystemGroup($groupname){
		$rs = new Result();
		if((Session::getRoleOfArea( "AccountManagement"  ) >=  (3)  ) ){
			$amng = FunctionData::readGroupsManager();
			if(!FunctionData::isSystemGroupExists($groupname)){
				$amng->addSystemGroup($groupname);
				FunctionData::writeGroupsManager($amng);
				$aobj = new DataGroup();
				FunctionData::writeGroup($groupname,$aobj);
				$uobj = new DataGroupUsers();
				FunctionData::writeGroupUsers($groupname,$uobj);
				$rs->return = true;
			}else{
				$rs->addError(9 ,"Exists name");
			}
		}else{
			{Error::getInstance() ->addError( 16  , "Action not allow!"  );} ;
		}
		return $rs;
	}
	static function addUserToGroup($user_email,$group_name){
		$rs = new Result();
		if((Session::getRoleOfArea( "AccountManagement"  ) >=  (3)  ) ){
			if(FunctionData::checkEmailRegisted($user_email) && FunctionData::isSystemGroupExists($group_name)){
				if($users = FunctionData::readGroupUsers($group_name)){
					$users->addUser($user_email);
					FunctionData::writeGroupUsers($group_name,$users);
					$join_groups = FunctionData::readUserGroups($user_email);
					$join_groups->addGroup($group_name);
					FunctionData::writeUserGroups($user_email,$join_groups);
					$rs->return = true;
				}else{
					$rs->addError(12 ,"Unknown Error Please contact Administrator!");
				}
			}else{
				$rs->addError(11 ,"Invalid Email or Group not exists!");
			}
		}else{
			{Error::getInstance() ->addError( 16  , "Action not allow!"  );} ;
		}
		return $rs;
	}
	static function setLicenseToGroup($license,$group_name){
		$rs = new Result();
		if((Session::getRoleOfArea( "AccountManagement"  ) >=  (5)  ) ){
			{Error::getInstance() ->addError( -2 , "read group $group_name" );} ;
			$group = FunctionData::readGroup($group_name);
			{Error::getInstance() ->addError( -2 , "finish read group" );} ;
			if($group!=null){
				$group->addRole($license->getArea(),$license->Role);
				FunctionData::writeGroup($group_name,$group);
				$rs->return = true;
			}else{
				{Error::getInstance() ->addError( 13  , "Group Not Exists!" );} ;
			}
		}else{
			{Error::getInstance() ->addError( 16  , "Action not allow!"  );} ;
		}
		return $rs;
	}
}
?>